<?php 
class document{
		protected 
			$document   = "",
			$attribute  = "";

	function __construct(){}
	
	
	 /**
	 * Func: escape_line
	 * Nota: Escapa los salto de line "\n" para poder imprimir en un textarea
	 * y si seleccionamos para escapar para un script de escapa lo siguiente
	 * "\n" salto de linea, "\t" tabulador, "\r" retorno de carro...
	 **/
	 private function escape_line($string,$line_script=false){
		  if(count($a_string = explode("\n",$string)) >= 2 ):{
				//echo "\n";
				$n=0;
				foreach($a_string as $str):{
					 if(!$line_script):{
						  $n++;
						  $line=($n<count($a_string))?"\\n":"";
						  if($str):{
								@$str_tmp .= $str.$line; 
						  }endif;
					 }else:{
						  for($i=0;$i<strlen($str);$i++):{
								if(preg_match('/([^\\t\\r\\n])/i',$str[$i],$COINCIDEN)){
									 @$str_tmp .= $str[$i];
								}
						  }endfor;
					 }endif;
				}endforeach;
				$string=$str_tmp;
		  }endif;
		  return $string;
	 }
	
	function value($nameElementCreated,$string){
			$this->attribute .=  'document.getElementById("'.$nameElementCreated.'").value="'.utf8_encode( str_replace('"','\\"',$string) ).'";';
			$this->create();
	}
	
	function attribute($nameElementCreated,$nameAttribute,$string=""){
			$this->attribute .=  'jQuery("#'.$nameElementCreated.'").attr("'.$nameAttribute.'","'.utf8_encode( str_replace('"','\\"',$string) ).'");';
			$this->create();
	}

	function removeAttr($nameElementCreated,$nameAttribute){
			$this->attribute .=  'jQuery("#'.$nameElementCreated.'").removeAttr("'.$nameAttribute.'");';
			$this->create();
	}
	
	function innerHTML($nameElementCreated,$string){
			$this->attribute .=  'document.getElementById("'.$nameElementCreated.'").innerHTML="'.utf8_encode( str_replace('"','\\"',$this->escape_line($string,true)) ).'";';
			$this->create();
	}
	

	function open($url="",$window="",$params=""){
		 $this->document .= 'window.open("'.str_replace('"','\\"',$url).'","'.str_replace('"','\\"',$window).'","'.str_replace('"','\\"',$params).'");';
		 $this->create();
	}
	
	function reload(){
		 $this->document .= 'window.location.reload();';
		 $this->create();
	}
	
	function alert($string){
		 $this->document .= 'alert("'.utf8_encode( str_replace('"','\\"',$string) ).'");';
		 $this->create();
	}
	
	function script($string){
		 $this->document .= utf8_encode( $this->escape_line($string,true) );
		 $this->create();
	}
	
	function append($nameElementCreated,$string){
		 $this->document .=  'var tempInnerHTML = document.getElementById("'.$nameElementCreated.'").innerHTML;';
		 $this->document .=  'document.getElementById("'.$nameElementCreated.'").innerHTML="'.utf8_encode( str_replace('"','\\"',$this->escape_line($string,true)) ).'"+tempInnerHTML;';
		 $this->create();
	}

	function prepend($nameElementCreated,$string){
		 $this->document .=  'var tempInnerHTML = document.getElementById("'.$nameElementCreated.'").innerHTML;';
		 $this->document .=  'document.getElementById("'.$nameElementCreated.'").innerHTML=tempInnerHTML+"'.utf8_encode( str_replace('"','\\"',$this->escape_line($string,true)) ).'";';
		 $this->create();
	}




	function createContainer($name){
			$this->document .= 'document.getElementById("'.$name.'").innerHTML="";';
			$this->document .= 'var '.$name.'=document.getElementById("'.$name.'");';
			$this->create();
	}

	function createElement($name,$typeElementCreate){
		 $this->document .= 'var '.$name.' = document.createElement("'.$typeElementCreate.'");';
		 $this->document .=  $name.'.id="'.$name.'";';
		 $this->create();
	}
	
	function createTextNode($name,$string){
		 $this->document .= 'var '.$name.' = document.createTextNode("'.utf8_encode( str_replace('"','\\"',$string) ).'");';
		 $this->create();
	}
	
	function appendChild($nameElementCreated,$nameElementinsert){
		 $this->document .=  $nameElementCreated.'.appendChild('.$nameElementCreatedAdd.');';
		 $this->create();
	}
	

	function prependChild($nameElementCreated,$nameElementCreatedAdd){
		 $this->document .=
			'function prependChild(e,i){'. 
				'e.insertBefore(i,e.firstChild);'. 
			'};'.
			'prependChild('.$nameElementCreated.','.$nameElementCreatedAdd.');';
		$this->create();
	}

	
	function insertChildAfter($nameElementCreated,$nameElementinsert){
		$this->document .=
			'function insertChildAfter(e,i){'.
				'if(e.nextSibling){e.parentNode.insertBefore(i,e.nextSibling);}else{e.parentNode.appendChild(i);};'. 
			'};'. 
			'insertChildAfter('.$nameElementCreated.','.$nameElementinsert.');';
		$this->create();
	}
	
	
	function create(){
			echo
			'<script type="text/javascript" language="javascript">'.
			'setTimeout(function(){'.$this->document.'},20);'.
			'setTimeout(function(){'.$this->attribute.'},20);'.
			'</script>';
			$this->document = $this->attribute = "";
	}
}
?> 




